package com.yan.utils;

import java.text.MessageFormat;

import org.apache.log4j.Logger;

public class LogUtil {

	static Logger logger = Logger.getLogger(LogUtil.class);

	public static String getStackMessage(Exception e) {
		StringBuilder error = new StringBuilder();
		StackTraceElement[] stacks = e.getStackTrace();
		for (StackTraceElement stack : stacks) {
			String className = stack.getClassName();
			StringBuffer buffer = new StringBuffer();
			buffer.append("className:" + className);
			buffer.append("\t method:" + stack.getMethodName());
			buffer.append("\t line:" + stack.getLineNumber());
			error.append(buffer.toString());
		}
		System.out.println(error.toString());
		return error.toString();
	}

	/**
	 * 打印log
	 *
	 * @param log
	 * @param arguments
	 */
	public static void printInfo(Logger logg, String log, Object... arguments) {
		logg.info(logMsg(log, arguments));
	}

	/**
	 * @category 打印带异常的日志
	 * @since 个人中心
	 * @author Yue Chang
	 * @date 2017年6月3日 下午3:09:01
	 * @param logg
	 * @param exception
	 * @param log
	 * @param arguments
	 */
	public static void printInfo(Logger logg, Exception exception, String log, Object... arguments) {
		logg.info(logMsg(log, arguments), exception);
	}

	/**
	 * 打印log
	 */
	public static void printError(Logger logg, String log, Object... arguments) {
		logg.error(logMsg(log, arguments));
	}

	/**
	 * @category 打印带异常的错误日志
	 * @since 个人中心
	 * @author Yue Chang
	 * @date 2017年6月3日 下午3:09:01
	 * @param logg
	 * @param exception
	 * @param log
	 * @param arguments
	 */
	public static void printError(Logger logg, Exception exception, String log, Object... arguments) {
		logg.error(logMsg(log, arguments), exception);
	}

	/**
	 * 打印log
	 */
	public static void printWarn(Logger logg, String log, Object... arguments) {
		logg.warn(logMsg(log, arguments));
	}

	/**
	 * @category 打印带异常的警告日志
	 * @since 个人中心
	 * @author Yue Chang
	 * @date 2017年6月3日 下午3:09:01
	 * @param logg
	 * @param exception
	 * @param log
	 * @param arguments
	 */
	public static void printWarn(Logger logg, Exception exception, String log, Object... arguments) {
		logg.warn(logMsg(log, arguments), exception);
	}

	private static String logMsg(String log, Object... arguments) {
		String message = MessageFormat.format(log, arguments);
		return message;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			int i = 5 / 0;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getStackMessage(e);
		}

		// EnumUtil.ChatType chatType = EnumUtil.ChatType.fromString(type);
		if (EnumUtil.ChatType.fromString("basketball") != null) {
			System.out.println(EnumUtil.ChatType.fromString("basketball"));
		}
		System.out.println();

		String requestURI = "requestURI";
		String sign = "sign";
		String clientSign = "clientSign";
		String clientSignE = "clientSign.equals(sign)";

		printInfo(logger, "请求URI:[{0}]", requestURI);

	}

}
